package com.example.zce.library.data.sys.service;

import com.alibaba.fastjson.JSON;
import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.common.utils.UUIDUtils;
import com.example.zce.library.data.sys.constant.BackMenuEnum;
import com.example.zce.library.data.sys.dao.BackMenuMapper;
import com.example.zce.library.data.sys.dao.BackRoleMenuMapper;
import com.example.zce.library.data.sys.entity.BackMenu;
import com.example.zce.library.data.sys.entity.BackRole;
import com.example.zce.library.data.sys.entity.BackRoleMenu;
import com.example.zce.library.data.sys.model.BackMenuModel;
import com.example.zce.library.data.sys.model.BackPermissionModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/7/25 22:33
 */
@Service
public class BackMenuService {

    @Autowired
    private BackMenuMapper backMenuMapper;

    @Autowired
    private BackRoleMenuMapper backRoleMenuMapper;


    /**
     * 保存或修改
     * @param backMenu
     * @return
     */
    @Transactional
    public int saveOrUpdate(BackMenu backMenu) throws Exception{
        if (StringUtils.isNotEmpty(backMenu.getId())){
            return backMenuMapper.updateByPrimaryKeySelective(backMenu);
        }else{
            backMenu.setId(UUIDUtils.getUUID32());
            if (backMenu.getType().equals(BackMenuEnum.TYPE_MENU.getIntegerValue())){
                backMenu.setParentId(BackMenuEnum.PARENT_ID.getStringValue());
            }
            return backMenuMapper.insert(backMenu);
        }
    }
    
    public ResultTableBean<BackMenu> findByParams(Map params){
        PageHelper.startPage(Integer.valueOf(params.get("pageNum").toString()), Integer.valueOf(params.get("pageSize").toString()));
        List<BackMenu> backMenus = backMenuMapper.selectListByParams(params);
        PageInfo<BackMenu> pageInfo = new PageInfo<>(backMenus);
        ResultTableBean<BackMenu> resultTableBean = new ResultTableBean<>(0,backMenus,pageInfo.getTotal());
        return resultTableBean;
    }

    public BackMenu findById(String id){
        return backMenuMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询已经启用的树形结构菜单
     * @return
     */
    public List<BackMenuModel> getBackMenuTree(){
        List<BackMenuModel> backMenuModels=new ArrayList<>();
        Map paramsMenu=new HashMap();
        paramsMenu.put("type",BackMenuEnum.TYPE_MENU.getIntegerValue());
        paramsMenu.put("state",BackMenuEnum.STATE_OK.getIntegerValue());
        List<BackMenu> backMenus = backMenuMapper.selectListByParams(paramsMenu);
        for (BackMenu params:backMenus) {
            BackMenuModel backMenuModel=new BackMenuModel();
            setBackMenuModel(params, backMenuModel);
            paramsMenu.put("type",BackMenuEnum.TYPE_PAGE.getIntegerValue());
            paramsMenu.put("parentId",params.getId());
            List<BackMenu> backPages = backMenuMapper.selectListByParams(paramsMenu);
            if (!backPages.isEmpty()){
                for (BackMenu page:backPages) {
                    BackMenuModel backPageModel=new BackMenuModel();
                    setBackMenuModel(page, backPageModel);
                    backMenuModel.getBackMenuModelList().add(backPageModel);
                }
            }
            backMenuModels.add(backMenuModel);
        }
        return backMenuModels;
    }

    public List<BackPermissionModel> getPermissionTree(String roleId){
        //查询改角色拥有的权限
        Map params=new HashMap();
        params.put("roleId",roleId);
        List<BackRoleMenu> backRoleMenus = backRoleMenuMapper.selectListByParams(params);

        List<BackPermissionModel> backPermissionModels=new ArrayList<>();
        Map paramsMenu=new HashMap();
        paramsMenu.put("type",BackMenuEnum.TYPE_MENU.getIntegerValue());
        paramsMenu.put("state",BackMenuEnum.STATE_OK.getIntegerValue());
        List<BackMenu> backMenus = backMenuMapper.selectListByParams(paramsMenu);
        for (BackMenu menu:backMenus) {
            BackPermissionModel backPermissionModel=new BackPermissionModel();
            backPermissionModel.setId(menu.getId());
            backPermissionModel.setPid(menu.getParentId());
            backPermissionModel.setName(menu.getName()+" | "+menu.getCode());
            backPermissionModel.setOpen(true);
            for (BackRoleMenu brm:backRoleMenus) {
                if (menu.getId().equals(brm.getMenuId())){
                    backPermissionModel.setChecked(true);
                }
            }
            backPermissionModels.add(backPermissionModel);

            paramsMenu.put("type",BackMenuEnum.TYPE_PAGE.getIntegerValue());
            paramsMenu.put("parentId",menu.getId());
            List<BackMenu> backPages = backMenuMapper.selectListByParams(paramsMenu);
            for (BackMenu page:backPages) {
                BackPermissionModel backPermissionSonModel=new BackPermissionModel();
                backPermissionSonModel.setId(page.getId());
                backPermissionSonModel.setPid(page.getParentId());
                backPermissionSonModel.setName(page.getName()+" | "+page.getCode()+" | "+page.getPath());
                for (BackRoleMenu brm:backRoleMenus) {
                    if (page.getId().equals(brm.getMenuId())){
                        backPermissionSonModel.setChecked(true);
                    }
                }
                backPermissionModels.add(backPermissionSonModel);
            }
        }
        return backPermissionModels;
    }

    @Transactional
    public int delete(String id) throws Exception{
        backMenuMapper.deleteByPrimaryKey(id);
        Map params=new HashMap();
        params.put("parentId",id);
        List<BackMenu> backMenus = backMenuMapper.selectListByParams(params);
        for (BackMenu bm:backMenus) {
            backMenuMapper.deleteByPrimaryKey(bm.getId());
        }
        return 1;
    }

    /**
     * 实体和MODEL的转换
     * @param params
     * @param backMenuModel
     */
    private void setBackMenuModel(BackMenu params, BackMenuModel backMenuModel) {
        backMenuModel.setId(params.getId());
        backMenuModel.setCode(params.getCode());
        backMenuModel.setIcon(params.getIcon());
        backMenuModel.setName(params.getName());
        backMenuModel.setParentId(params.getParentId());
        backMenuModel.setPath(params.getPath());
        backMenuModel.setRemark(params.getRemark());
        backMenuModel.setSort(params.getSort());
        backMenuModel.setState(params.getState());
        backMenuModel.setType(params.getType());
        backMenuModel.setBackMenuModelList(new ArrayList<>());
    }
}
